package cc.mcggdd.leah.boot.stock.controller;

import cc.mcggdd.leah.boot.common.result.PageResult;
import cc.mcggdd.leah.boot.common.result.Result;
import cc.mcggdd.leah.boot.stock.model.vo.StockQuotesVO;
import cc.mcggdd.leah.boot.stock.model.vo.StockReportVO;
import cc.mcggdd.leah.boot.stock.model.vo.StockTradingVO;
import cc.mcggdd.leah.boot.stock.service.*;
import cn.hutool.http.HttpRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "报表统计接口")
@RestController
@RequestMapping("/api/v1/stockReport")
@RequiredArgsConstructor
public class StockReportController {
    private final StockDebtService stockDebtService;
    private final StockBillService stockBillService;
    private final StockInitCapitalService stockInitCapitalService;
    private final StockTransferService stockTransferService;
    private final StockReceivableService stockReceivableService;
    private final StockBondReverseRepoService stockBondReverseRepoService;

    @Operation(summary = "查询当前统计信息")
    @GetMapping
//    @PreAuthorize("@ss.hasPerm('stock:stockReport:query')")
    public Result<StockReportVO> getStockTradingPage() {
        StockReportVO report = new StockReportVO();
        report.setInitial(stockInitCapitalService.getStockInitCapitalsTotalAmount());
        report.setTransfer(stockTransferService.getStockTransferTotalAmount());
        report.setRepay(stockBillService.getStockTotalAmount(null, "N"));
        report.setDebtFee(stockDebtService.getStockDebtTotalFee());
        report.setStockCash(BigDecimal.valueOf(100000).add(stockBondReverseRepoService.getStockBondReverseRepoTotalAmount()));
        report.setOuterCash(stockReceivableService.getStockReceivableTotalAmount("N"));
        return Result.success(report);
    }

    @Operation(summary = "查询股票行情")
    @GetMapping("/{code}/quotes")
    public Result<StockQuotesVO> queryQuotes(@Parameter(description = "股票代码") @PathVariable String code) {
        if (code.startsWith( "30") || code.startsWith( "00" ) || code.startsWith( "12" )) {
            code = "sz" + code;
        }else {
            code = "sh" + code;
        }
        String Url = "http://hq.sinajs.cn/list=" + code;
        String response = HttpRequest.get(Url).header("Referer", "https://finance.sina.com.cn").execute().body();
        String temp = response.split("\"")[1];
        if (temp.length()<=0)
            return Result.success(null);
        String[] items = response.split("\"")[1].split(",");

        StockQuotesVO result = new StockQuotesVO();
        result.setCode(code);
        result.setName(items[0]);
        result.setNow(BigDecimal.valueOf(Double.parseDouble(items[3])));
        result.setOpen(BigDecimal.valueOf(Double.parseDouble(items[1])));
        result.setClose(BigDecimal.valueOf(Double.parseDouble(items[2])));
        result.setLow(BigDecimal.valueOf(Double.parseDouble(items[5])));
        result.setHigh(BigDecimal.valueOf(Double.parseDouble(items[4])));
        return Result.success(result);
    }
}
